﻿using DyUi.Base.Interfaces;
using DyUi.Base.Interfaces.SubSystems;
using DyUi.Base.SubSystems;
using DyUi.Core.SubSystems;

namespace DyUi.Core
{
    public class DyUiController : IDyUiController
    {
        public ISettingsSubSystem SettingsSubSystem { get; set; }
        
        public IExecutionSubSystem ExecutionSubSystem{ get; set; }

        public IMessageSubSystem MessageSubSystem { get; set; }

        public IControlSubSystem ControlSubSystem { get; set; }

        public IServiceSubSystem ServiceSubSystem { get; set; }

        public IAppletSubSystem AppletSubSystem { get; set; }

        public void Prepare()
        {
            if (SettingsSubSystem == null) SettingsSubSystem = new SettingsSubSystem();
            if (ExecutionSubSystem == null) ExecutionSubSystem = new ExecutionSubSystem();
            if (ControlSubSystem == null) ControlSubSystem = new ControlSubSystem();
            if (ServiceSubSystem == null) ServiceSubSystem = new ServiceSubSystem();
            if (MessageSubSystem == null) MessageSubSystem = new MessageSubSystem();
            if (AppletSubSystem == null) AppletSubSystem = new AppletSubSystem();

            SettingsSubSystem.Controller = this;
            ExecutionSubSystem.Controller = this;
            MessageSubSystem.Controller = this;
            ControlSubSystem.Controller = this;
            ServiceSubSystem.Controller = this;
            AppletSubSystem.Controller = this;

            SettingsSubSystem.Prepare();
            ExecutionSubSystem.Prepare();
            MessageSubSystem.Prepare();
            ControlSubSystem.Prepare();
            ServiceSubSystem.Prepare();
            AppletSubSystem.Prepare();
        }
        
        public void Start()
        {
            MessageSubSystem.SubscribeToEvent(null, (a, b) => a == "Controller.CreateAppletInstance", AppletSubSystem.CreateAppletInstance);
            //EventSubSystem.SubscribeToEvent(null, (a, b) => a == "Controller.Print", Print);
        }
    }
}
